package com.dd.web.base.service.impl;

import com.dd.common.entity.ResultCode;
import com.dd.common.exception.BussinessException;
import com.dd.framework.security.jwt.JwtTokenHelper;
import com.dd.web.base.dao.UmsAdminDao;
import com.dd.web.base.entity.common.CommonPage;
import com.dd.web.base.entity.data.UmsAdmin;
import com.dd.web.base.entity.data.UmsMenu;
import com.dd.web.base.entity.data.UmsRole;
import com.dd.web.base.entity.view.AdminUserDetails;
import com.dd.web.base.service.UmsAdminService;
import com.dd.web.base.service.UmsMenuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 查询管理员信息
 */
@Slf4j
@Service
public class UmsAdminServiceImpl implements UmsAdminService {


    @Autowired
    private UmsAdminDao adminDao;

    @Autowired
    private UmsMenuService umsMenuService;


//    @Autowired
//    private PasswordEncoder passwordEncoder;

    @Autowired
    private JwtTokenHelper jwtTokenHelper;


    @Override
    public String login(String username, String password) {
        String token = null;
        //密码需要客户端加密后传递
        AdminUserDetails userDetails = loadUserByUsername(username);
//        if(!passwordEncoder.matches(password,userDetails.getUmsAdmin().getPassword())){
//            throw new BadCredentialsException("密码不正确");
//        }
        if(userDetails == null || (!password.equals(userDetails.getUmsAdmin().getPassword()))){
            throw new BussinessException(ResultCode.ADMIN_LOGIN_PASS);
        }

        //登录成功，设置springsecurity
        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails,
                null, userDetails.getAuthorityList());
        SecurityContextHolder.getContext().setAuthentication(authentication);

        token = jwtTokenHelper.createToken(userDetails.getUmsAdmin().getUsername());
        return token;
    }

    @Override
    public CommonPage<UmsAdmin> queryAdminList(int pageNo, int pageSize) {
        Page page = PageHelper.startPage(pageNo,pageSize);
        List<UmsAdmin> adminList = adminDao.selectAdminList();
        CommonPage<UmsAdmin> commonPage = CommonPage.restPage(adminList);
        return commonPage;
    }

    //    @Override
    public List<UmsAdmin> queryAdminsByName(String name) {
        return adminDao.selectAdminsByName(name);
    }

    @Override
    public AdminUserDetails loadUserByUsername(String username){
        //获取用户信息
        UmsAdmin admin = getAdminByUsername(username);
        if (admin != null) {
            List<UmsMenu> menuList = null; //umsRoleRelationDao.getResourceList(admin.getId());
            AdminUserDetails userDetail = new AdminUserDetails(admin,menuList);
//            userDetail.setRoleList(umsRoleRelationDao.getRoleListByAdminId(admin.getId()));
            return userDetail;
        }
        return null;
    }

    @Override
    public UmsAdmin getAdminByUsername(String username) {
        List<UmsAdmin> adminList = adminDao.selectAdminsByName(username);
        if (adminList != null && adminList.size() > 0) {
            return adminList.get(0);
        }
        return null;
    }

    @Override
    public List<UmsRole> getRoleList(Long adminId) {
        return null; //umsRoleRelationDao.getRoleListByAdminId(adminId);
    }

    @Override
    public List<UmsMenu> getResourceList(Long adminId) {
        return null; //umsRoleRelationDao.get(adminId);
    }


}
